package schedule;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.exception.RemotingException;

import static schedule.ScheduleMessageConsumer.NAMESRV_ADDR;
import static schedule.ScheduleMessageConsumer.TEST_TOPIC_2;

public class ScheduleMessageProducer {

    public static void main(String[] args)
            throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer(TEST_TOPIC_2 + "schedual-message");
        producer.setNamesrvAddr(NAMESRV_ADDR);
        producer.setVipChannelEnabled(false);
        producer.setUnitMode(true);

        /**
         * clientId 的生成策略如下：
         *
         * clientIp：客户端的 IP 地址。
         * instanceName：实例名称，默认值为 DEFAULT，但在真正 clientConfig 的 getInstanceName 方法时如果实例名称为 DEFAULT，会自动将其替换为进程的 PID。
         * unitName：单元名称，如果不为空，则会追加到 clientId 中。
         */
        producer.setUnitName("TestCluster");
        producer.start();
        int totalMessageToSend = 10;
        for (int i = 0; i < totalMessageToSend; i++) {
            Message message = new Message(TEST_TOPIC_2, ("Hello scheduled message" + i).getBytes());
//            message.setDelayTimeLevel(3);
            producer.send(message);

        }
        producer.shutdown();
    }
}
